package com.appspot.gaeforum309.db;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

// TODO: Add a mechanism to check all objects of data base, to found for example orphean object.
// The system use a field "CheckingIteration" present on all tables. This field make possible to know what object is checked and what 
// object isn't checked for this iteration.
// After, it get a batch of unchecked objects of one table, and he check it.
// The check mainly check if related object exist (for a message, DBUser of the author and DBConversation shall be present in table).

// TODO: Add a memcache mechanism for frequently used object.
// Cache mechanisme only for : DBUser, DBCategory, DBTopic, DBConversation, DBConfig.
// We store more information for an object than the data base :
//    - DBConversation store author key and first message date.
//    - DBTopic store last conversation key
//    - ...
// 

// TODO: Add a mechanism to save / restore data base information.
// Save / restore can be save:
//    - different level of data (only cat / topic, or with user and message)
//    - all data or only recent data (for example, only latest 30 days conversations / messages)
// save / restore can be used to save date in older data base format and restore it in the newer. For this, new field will be fill with
// a default value.
public final class PMF {
    private static final PersistenceManagerFactory pmfInstance =
        JDOHelper.getPersistenceManagerFactory("transactions-optional");

    private static PersistenceManager _pm = null;
    
    private PMF() {}
    
    public static PersistenceManagerFactory get() {
        return pmfInstance;
    }
    
    public static PersistenceManager pm() {
    	if(_pm == null)
    		_pm = get().getPersistenceManager();
    	
    	return _pm;
    }
    
    public static void close()
    {
    	if(_pm != null)
    	{
    		_pm.close();
    		_pm = null;
    	}
    }
}
